package user_interaction;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import dca.BadDataException;
import dca.Severity;
import dca.Symptom;
import dca.SymptomRegistry;


public class Symptom_GUI extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGTH = 350;
	
	private JTextArea display;
	private JScrollPane displayPane;
	private JTextField inputField;
	private JComboBox<?> severityField;
	private JButton AddButton;
	
	private SymptomRegistry SymptomBase;
	
	public Symptom_GUI(SymptomRegistry SymptomBase){
		this.SymptomBase = SymptomBase;
		
		
		//add visual components
		
		display = new JTextArea(15,23);
		displayPane = new JScrollPane(display);
		inputField = new JTextField(10);
		String [] severityString = new String[Severity.values().length];
		int i=0;
		for (Severity s: Severity.values())
		{
			severityString[i]=s.toString();
			i++;
		}
		severityField = new JComboBox<Object>(severityString);;
		AddButton = new JButton("Add Symptom");
		AddButton.addActionListener(new AddButtonListener());
		setLayout(new FlowLayout());
		setSize(FRAME_WIDTH,FRAME_HEIGTH);
		add(displayPane);
		add(inputField);
		add(severityField);
		add(AddButton);
		

		//Add content to display
		try {
			updateDisplay();
		} catch (BadDataException e) {
			//System.out.println(e.getMessage());
		}
		
	}
	
	public void updateDisplay() throws BadDataException{
		String SymptomList="";
		SymptomBase.setOrder("Alphabet");
		for(Symptom s:SymptomBase.SymptomList())
		{
			SymptomList+=s.toString()+"\n";
		}
		display.setText(SymptomList);
		
	}
	
	class AddButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0)  {
			String name = inputField.getText();
			String severity = (String) severityField.getSelectedItem();
			inputField.setText("");
			try {
				SymptomBase.addSymptom(new Symptom(name,severity));
				SymptomBase.saveSymptoms();
				updateDisplay();
			} catch (BadDataException e) {
					System.out.println(e.getMessage());
			} catch (FileNotFoundException e)
			{
				e.printStackTrace();
			}		
			 
			
		}
		
	}
	

}
